// index.js
import {
  get,
  post
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import {
  setBallSystem,
} from '../../utils/util';
import createSmyooApi from '../../utils/smyoo-api';
// 获取应用实例
const app = getApp()

Page({
  data: {
    amount: 8888.09,
    coupon: 2,
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    isShowShop: true,
    bannerList: [],
    userInfo: {},
    currentIndex: 1,
    shopArray: [],
    shopIds: [],
    isShowTip: false
  },
  onLoad() {

  },
  onShow() {
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    });
    console.log('userInfo', userInfo)
    if (userInfo.id) {
      this.getShopData();
      this.getBannerList();
    }
  },
  getShopData() {
    get(APIS.shop).then(res => {
      const list = res.data;
      if (!list.length) {
        return []
      }
      const sid = wx.getStorageSync('currentShop') || list[0].id;
      const shopArray = list.map(x => x.name);
      const shopIds = list.map(x => x.id);
      const currentIndex = shopIds.findIndex(x => x === sid);
      this.setData({
        shopArray,
        shopIds,
        currentIndex
      });
      wx.setStorageSync('currentShop', sid);
      wx.setStorageSync('currentShopName', shopArray[currentIndex]);
    })
  },
  getBannerList() {
    post(APIS.banner, {
      type: 1
    }).then(res => {
      const list = res.data;
      this.setData({
        bannerList: list.map(x => x.img)
      });
    })
  },
  handleJumpToPay() {
    wx.navigateTo({
      url: '/subPackage/pages/pay'
    })
  },
  handleClose() {
    this.setData({
      isShowTip: false
    });
  },
  handleJumpToStage() {
    wx.scanCode({
      async success(res) {
        console.log(res);
        const url = '/' + res.path;
        // 兼容打开小程序二维码
        // if (url.includes('mp.weixin.qq.com')) {
        //   wx.navigateToMiniProgram({
        //     appId: url
        //   })
        // }
        wx.navigateTo({
          url
        })
      }
    })
  },
  handleJumpToCoupon() {
    wx.navigateTo({
      url: '/pages/coupon/index'
    })
  },
  handleJumpToOther() {
    wx.navigateTo({
      url: '/subPackagePay/pages/other'
    })
    // wx.showToast({
    //   icon: 'none',
    //   title: '请联系店长开台~'
    // })
  },
  handleJumpToShop() {
    wx.switchTab({
      url: '/pages/shop/index'
    })
  },
  bindChangeShop(e) {
    const index = e.detail.value;
    this.setData({
      currentIndex: index
    });
    wx.setStorageSync('currentShop', this.data.shopIds[index]);
    wx.setStorageSync('currentShopName', this.data.shopArray[index]);
  },
  closePop() {
    this.setData({
      isShowShop: false
    });
  },
  async handleJumpToList() {
    // const _classSmyooApi = createSmyooApi({})
    // // setBallSystem('SYN_DF7923', 'C23789186D4C4B0B25DBAA5AAEE09454', 0);
    // // console.log('-----');
    // await _classSmyooApi.immediateApi()
    // // 获取我的设备列表
    // const list = await _classSmyooApi.apiQueryDevices();
    wx.navigateTo({
      url: '/subPackage/pages/list'
    })
  },
  onShareAppMessage() {
    return getApp().globalData.shareMessage
  }
})